home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Binary iostream class / bstream++.sit / bstream++ / bstream.cp next >
Text File  |  1993-08-11  |  4KB  |  160 lines

  1. // bstream.cp -- Implementation of bstream class.
  2. //
  3. // These are the insterters and extractors for the built in types.
  4. //
  5. //    ComputKit
  6. //    Kitzel Hoover (KitzelH@aol.com)
  7. //    August 9, 1993
  8. //
  9. // You are free to use, copy, and modify this code.  However, please
  10. // report any bugs to me.
  11. //
  12. #include <fstream.h>
  13. #include "bstream.h"
  14.  
  15. int strlen (const char* string) {
  16.     int count = 0;
  17.     while (*string++) count++;
  18.     return count;
  19. }
  20.  
  21. // Inserters ------------------------------------------------------
  22.  
  23. bstream & bstream::operator<<(const char *string) {
  24.     unsigned char len = strlen(string);
  25.     if (string_width && (string_width < len)) 
  26.         len = string_width;                        // truncate string
  27.     fstream::write(&len,sizeof(len));            // write length byte
  28.     fstream::write(string,len);                    // write string
  29.     if (string_width && (string_width > len))
  30.         for (int i = string_width - len; i > 0; i--) 
  31.             fstream::put('\0');                    // pad if needed
  32.     if (string_width) string_width = 0;            // reset to default
  33.     return *this;
  34. };
  35.  
  36. bstream &bstream::operator<<(char c) {
  37.     fstream::put(c);
  38.     return *this;
  39. };
  40.  
  41. bstream &bstream::operator<<(short v) {
  42.     fstream::write(&v,sizeof(v));
  43.     return *this;
  44. }
  45. bstream &bstream::operator<<(int v) {
  46.     fstream::write(&v,sizeof(v));
  47.     return *this;
  48. }
  49.  
  50. bstream &bstream::operator<<(long v) {
  51.     fstream::write(&v,sizeof(v));
  52.     return *this;
  53. }
  54.  
  55. bstream &bstream::operator<<(unsigned short v) {
  56.     fstream::write(&v,sizeof(v));
  57.     return *this;
  58. }
  59.  
  60. bstream &bstream::operator<<(unsigned v) {
  61.     fstream::write(&v,sizeof(v));
  62.     return *this;
  63. }
  64.  
  65. bstream &bstream::operator<<(unsigned long v) {
  66.     fstream::write(&v,sizeof(v));
  67.     return *this;
  68. }
  69.  
  70. bstream &bstream::operator<<(float v) {
  71.     fstream::write(&v,sizeof(v));
  72.     return *this;
  73. }
  74.  
  75. bstream &bstream::operator<<(double v) {
  76.     fstream::write(&v,sizeof(v));
  77.     return *this;
  78. }
  79.  
  80. bstream &bstream::operator<<(long double v) {
  81.     fstream::write(&v,sizeof(v));
  82.     return *this;
  83. }
  84.  
  85. // Extractors -------------------------------------------------------
  86.  
  87. bstream &bstream::operator>>(char *string) {
  88.     unsigned char len, trash;
  89.     fstream::read(&len, sizeof(len));
  90.     string[0] = '\0';                        // start with empty string
  91.     fstream::get(string,len+1,'\0');        // leaves null on stream
  92.     string[len] = 0;
  93.     if (string_width && (string_width>len))
  94.         for (int i = string_width - len; i>0; i--) {
  95.             fstream::get(trash);            // skip padding
  96.         }
  97.     if (string_width) string_width = 0;     // reset to default
  98.     return *this;
  99. }
  100.  
  101. bstream &bstream::operator>>(char &c) {
  102.     fstream::get(c);
  103.     return *this;
  104. }
  105.  
  106. bstream &bstream::operator>>(short &v) {
  107.     fstream::read (&v, sizeof(v) );
  108.     return *this;
  109. }
  110.  
  111. bstream &bstream::operator>>(int &v) {
  112.     fstream::read (&v, sizeof(v) );
  113.     return *this;
  114. }
  115.  
  116. bstream &bstream::operator>>(long &v) {
  117.     fstream::read (&v, sizeof(v) );
  118.     return *this;
  119. }
  120.  
  121. bstream &bstream::operator>>(unsigned short &v) {
  122.     fstream::read (&v, sizeof(v) );
  123.     return *this;
  124. }
  125.  
  126. bstream &bstream::operator>>(unsigned int &v) {
  127.     fstream::read (&v, sizeof(v) );
  128.     return *this;
  129. }
  130.  
  131. bstream &bstream::operator>>(unsigned long &v) {
  132.     fstream::read (&v, sizeof(v) );
  133.     return *this;
  134. }
  135.  
  136. bstream &bstream::operator>>(float &v) {
  137.     fstream::read (&v, sizeof(v) );
  138.     return *this;
  139. }
  140.  
  141. bstream &bstream::operator>>(double &v) {
  142.     fstream::read (&v, sizeof(v) );
  143.     return *this;
  144. }
  145.  
  146. bstream &bstream::operator>>(long double &v) {
  147.     fstream::read (&v, sizeof(v) );
  148.     return *this;
  149. }
  150.  
  151. // Manipulators ----------------------------------------------------
  152. bstream& operator<< (bstream& bs, swidth& sw) {
  153.     bs.setwidth(sw.val);
  154.     return bs;
  155. }
  156.  
  157. bstream& operator>> (bstream& bs, swidth& sw) {
  158.     bs.setwidth(sw.val);
  159.     return bs;
  160. }